<?php
header('Content-Type: text/plain');
require_once dirname(__FILE__) . '/../../../incls/php/dbutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/miscutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/config.php';
session_name(SESSION);
session_start();
$myPdo = DbUtils::createPdoInst();
$action = $_POST['action'];
try {
  $myPdo->beginTransaction();
  switch ($action) {
    case 'save_cat':
      save_cat($myPdo);
      break;
    case 'del_cat':
      del_cat($myPdo);
      break;
    case 'up':
      up($myPdo);
      break;
    case 'down':
      down($myPdo);
      break;
    default:
      break;
  }
  $myPdo->commit();
} catch (PDOException $e) {
  echo $e->getMessage();
}

function save_cat($myPdo) {
  if (!isset($_SESSION['admin' . USER])) {
    return;
  }
  $q = json_decode(MiscUtils::getParam('q', '[]'));
  
  if (isset($q->id)) {
    DbUtils::update($myPdo,  TABLE_NAME_PREFIX_PLUGIN . basename(dirname(__FILE__)), $q);
  } else {
    $opt = new stdClass();
    $opt->select_expr = 'MAX(t.order) AS max';
    $q->order = DbUtils::get($myPdo,  TABLE_NAME_PREFIX_PLUGIN . basename(dirname(__FILE__)), NULL, NULL, NULL, NULL, NULL, $opt)->d[0]->max + 1;
    
    DbUtils::add($myPdo,  TABLE_NAME_PREFIX_PLUGIN . basename(dirname(__FILE__)), $q);
    $q->id = $myPdo->lastInsertId();
  }
  echo json_encode($q);
}

function del_cat($myPdo) {
  if (!isset($_SESSION['admin' . USER])) {
    return;
  }
  $q = MiscUtils::getParam('q', 0);
  $cond_vals = new stdClass(); 
  $cond_vals->c = 'id = :v1';
  $cond_vals->v = array(':v1' => $q);
  DbUtils::deleteByCond($myPdo,  TABLE_NAME_PREFIX_PLUGIN . basename(dirname(__FILE__)), $cond_vals);
  
  $cond_vals = new stdClass(); 
  $cond_vals->c = 'parent_id = :v1';
  $cond_vals->v = array(':v1' => $q);
  DbUtils::deleteByCond($myPdo,  TABLE_NAME_PREFIX_PLUGIN . basename(dirname(__FILE__)), $cond_vals);
  
  echo json_encode(array());
}

function up($myPdo) {
  if (!isset($_SESSION['admin' . USER])) {
    return;
  }
  $q = MiscUtils::getParam('q', 0);
  $cond_vals = new stdClass();
  $cond_vals->c = 't.id = :v1';
  $cond_vals->v = array(':v1' => $q);
  $tmp = DbUtils::get($myPdo,  TABLE_NAME_PREFIX_PLUGIN . basename(dirname(__FILE__)), $cond_vals, NULL, NULL, NULL, NULL, NULL)->d;
  if (count($tmp) > 0) {
    $item = $tmp[0];
    $cond_vals = new stdClass();
    $cond_vals->c = 't.order < :v1';
    $cond_vals->v = array(':v1' => $item->order);
    $opt = new stdClass();
    $opt->select_expr = 't.order';
    $items = DbUtils::get($myPdo,  TABLE_NAME_PREFIX_PLUGIN . basename(dirname(__FILE__)), $cond_vals, 't.order', 'DESC', 1, 2, $opt)->d;
    if (count($items) == 2) {
      $item->order = $items[0]->order - ($items[0]->order - $items[1]->order) / 2;
    } else if (count($items) == 1) {
      $item->order = $items[0]->order - 1;
    }
    DbUtils::update($myPdo,  TABLE_NAME_PREFIX_PLUGIN . basename(dirname(__FILE__)), $item);
  }
  echo json_encode(array());
}

function down($myPdo) {
  if (!isset($_SESSION['admin' . USER])) {
    return;
  }
  $q = MiscUtils::getParam('q', 0);
  $cond_vals = new stdClass();
  $cond_vals->c = 't.id = :v1';
  $cond_vals->v = array(':v1' => $q);
  $tmp = DbUtils::get($myPdo,  TABLE_NAME_PREFIX_PLUGIN . basename(dirname(__FILE__)), $cond_vals, NULL, NULL, NULL, NULL, NULL)->d;
  if (count($tmp) > 0) {
    $item = $tmp[0];
    $cond_vals = new stdClass();
    $cond_vals->c = 't.order > :v1';
    $cond_vals->v = array(':v1' => $item->order);
    $opt = new stdClass();
    $opt->select_expr = 't.order';
    $items = DbUtils::get($myPdo,  TABLE_NAME_PREFIX_PLUGIN . basename(dirname(__FILE__)), $cond_vals, 't.order', 'ASC', 1, 2, $opt)->d;
    if (count($items) == 2) {
      $item->order = $items[0]->order + ($items[1]->order - $items[0]->order) / 2;
    } else if (count($items) == 1) {
      $item->order = $items[0]->order + 1;
    }
    DbUtils::update($myPdo,  TABLE_NAME_PREFIX_PLUGIN . basename(dirname(__FILE__)), $item);
  }
  echo json_encode(array());
}
?>